


// 添加/更新数据
function setCookie(key, value, days = 1) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = key + "=" + encodeURIComponent(value) + expires + "; path=/";
}

// 删除数据
function deleteCookie(key) {
    setCookie(key, "", -1);
}

// 检查数据是否存在
function hasCookie(key) {
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i].trim();
        if (cookie.startsWith(key + "=")) {
            return true;
        }
    }
    return false;
}

function getCookieValue(key) {
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i].trim();
        if (cookie.startsWith(key + "=")) {
            return decodeURIComponent(cookie.substring(key.length + 1));
        }
    }
    return undefined;
}

function getPosition(node) {
    //获取元素相对于其父元素的left值var left
    var left = node.offsetLeft;
    var top = node.offsetTop;
    // 取得元素的offsetParent
    current = node.offsetParent;

    // 一直循环直到根元素
    while (current != null) {
        left += current.offsetLeft;
        top += current.offsetTop;
        current = current.offsetParent;
    }
    return {
        "left": left,
        "top": top
    }
}

async function float_text(msg, keepTime = 3000) {
    const floatingWindow = document.createElement("div");
    floatingWindow.classList.add("bottom-float-window");
    floatingWindow.innerHTML = `${msg}`;
    document.body.appendChild(floatingWindow);
    setTimeout(function () {
        floatingWindow.style.opacity = 0;
        setTimeout(function () {
            floatingWindow.remove();
        }, 300);
    }, keepTime);
}

function getRequestUrl(data, api) {
    let url = api + '?';
    for (const key in data) {
        if (data.hasOwnProperty(key)) {
            url += `${key}=${data[key]}&`;
        }
    }
    return url.slice(0, -1);
}

function fetchWithTimeout(url, options = {}, timeout = 8000) {
    return Promise.race([
        fetch(url, options),
        new Promise((_, reject) =>
            setTimeout(() => reject(new Error('timeout')), timeout)
        )
    ]);
}

function exit_login() {/*退出登入, 清除token, cache, 跳转到登入页面*/
    deleteCookie('token');
    window.location.href = "/";
}

function auth_verify() {
    const verifyAPI = '...';
    const fullUrl = getRequestUrl({ token: getCookieValue('token') }, verifyAPI);
    var legal = false;
    fetchWithTimeout(fullUrl, 5000)
        .then(response => response.json())
        .then(data => {
            if (data['msg'] == 100) {
                legal = true;
            }
        });
    if (!legal) {
        exit_login();
    }
}

/*  添加悬浮提示  */
const tooltip = document.createElement('div');
tooltip.className = 'tooltip';
document.body.appendChild(tooltip);
function add_float_tip(element, text, checkFunction = undefined) {
    element.addEventListener('mouseover', function (event) {
        if (!checkFunction || checkFunction()) {
            pos = getPosition(element);
            tooltip.style.display = 'block';
            tooltip.style.left = pos['left'] + this.offsetWidth + 10 + 'px';
            tooltip.style.top = pos['top'] + 6 + 'px';
            tooltip.innerText = text;
        }
    });
    element.addEventListener('mouseleave', function (event) {
        tooltip.style.display = 'none';
    });
}

const fullScreenTip = document.querySelector('.fullScreen-tip');
const fullScreenTipOverlay = document.querySelector('.fullScreen-tip-overlay');
const fullScreenTipBlink = document.querySelector('#fullScreen-tip-blink');
function showFullScreenTip(element) {
    fullScreenTip.style.display = 'block';
    document.body.style.overflow = 'hidden'; // 禁止滚动
    fullScreenTipBlink.innerHTML = element;
}

function hideFullScreenTip() {
    fullScreenTip.style.display = 'none';
    document.body.style.overflow = 'auto'; // 允许滚动
}

fullScreenTip.addEventListener('click', (event) => {
    if (event.target === fullScreenTipOverlay) {
        // 点击了黑色遮罩层
        fullScreenTipBlink.classList.add('fullScreen-tip-filter'); // 添加闪烁动画
        setTimeout(() => {
            fullScreenTipBlink.classList.remove('fullScreen-tip-filter');
        }, 200); // 1 秒后取消闪烁动画
    }
});

fullScreenTip.addEventListener('scroll', (event) => {
    event.stopPropagation(); // 阻止事件冒泡
});
